<template>

  <view class="weather">

    <view class="hourly_title">7天预报</view>
    <scroll-view
      scroll-x="true"
      class="daily"
    >
      <view
        class="d_item"
        v-for="(day,index) in forecast"
        :key="index"
      >
        <text class="d_txt">{{day.d_txt}}</text>
        <text class="d_date">{{day.d_date}}</text>
        <view class="h_img">
          <image
            mode="widthFix"
            :src="day.imgsrc_d"
          ></image>
        </view>
        <text class="h_tmp">{{day.tmp_min}}°~{{day.tmp_max}}°</text>
        <view class="h_img">
          <image
            mode="widthFix"
            :src="day.imgsrc_n"
          ></image>
        </view>
        <text class="d_wind_dir">{{day.wind_dir}}</text>
        <text class="d_wind_sc">{{day.wind_sc}}级</text>
      </view>

    </scroll-view>
  </view>

</template>

<script>
export default {
  name: 'Weather',
  props: {
    city: {
      type: String,
      default: ''
    }
  },
  data () {
    return {
      key: '104051ad798f43dba6ecf77f85d9d109',
      forecast: [],
      now: {
        city: '',
        tmp: '',
        imgsrc: '',
        wind_dir: '',
        wind_sc: '',
        hum: '',
        pres: ''
      },
      hourly: []
    }
  },
  created () {
    this.getForecast(this.city)
  },
  methods: {
    /**
     * 获取未来7天天气预报
     */
    getForecast (city) {
      var weekArray = ['周日', '周一', '周二', '周三', '周四', '周五', '周六']

      // 获取未来7天天气预报
      wx.request({
        url:
          'https://free-api.heweather.net/s6/weather/forecast?key=' +
          this.key +
          '&location=' +
          city,
        success: (result) => {
          // console.log(result)
          var arr = result.data.HeWeather6[0].daily_forecast
          var dailyForecast = []
          for (var i = 0; i < arr.length; i++) {
            dailyForecast[i] = {
              d_txt:
                i === 0 ? '今天' : weekArray[new Date(arr[i].date).getDay()],
              d_date: arr[i].date.substring(5),
              imgsrc_d: `https://moyv.top/wechat/images/weather/${arr[i].cond_code_d}.png`,
              imgsrc_n: `https://moyv.top/wechat/images/weather/${arr[i].cond_code_n}.png`,
              wind_dir: arr[i].wind_dir,
              wind_sc: arr[i].wind_sc,
              tmp_max: arr[i].tmp_max,
              tmp_min: arr[i].tmp_min,
              cond_txt_d: arr[i].cond_txt_d
            }
          }
          this.forecast = dailyForecast
        }
      })
    }
  }
}
</script>

<style lang="less">
.header {
  background-color: #64c8fa;
  /* background-image: linear-gradient(to right, #64a0f8, #64c8fa); */
  height: 450px;
  padding-top: 32px;
  text-align: center;
}

.top {
  display: flex;
  justify-content: space-between;
  align-content: center;
  align-items: center;
}

.city {
  text-align: center;
  color: white;
  display: inline-block;
  font-size: 52px;
  margin-left: 32px;
}

.search {
  margin-right: 32px;
  border-radius: 8px;
  display: inline-flex;
  justify-content: center;
  align-content: center;
  align-items: center;
  background-color: rgba(0, 0, 0, 0.1);
  height: 70px;
}

.search input {
  width: 200px;
  padding: 18px 32px;
  text-align: left;
  color: white;
  display: inline-block;
}

.bt_search {
  border-radius: 0 8px 8px 0;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.1);
  display: inline-flex;
  justify-content: center;
  align-content: center;
  align-items: center;
}

.bt_search icon {
  margin: 8px 18px;
}

.center {
  display: flex;
  justify-content: space-between;
  align-content: center;
  align-items: center;
}

.tmp {
  margin-left: 18px;
  display: inline-block;
  font-size: 180px;
  color: white;
}
.cond-image {
  width: 200px;
  margin-right: 32px;
  margin-top: 32px;
}
.bottom {
  display: flex;
  justify-content: space-between;
  align-content: center;
  align-items: center;
}
.bottom view {
  color: white;
  margin: 32px;
}

.hourly_title {
  font-weight: bold;
  font-size: 42px;
  padding: 18px 32px;
}
.hourly {
  width: 718px;
  margin: 0 18px;
  border-radius: 18px;
  box-shadow: 0.1rem 0.1rem 0.5rem rgba(0, 0, 0, 0.15);
  white-space: nowrap;
  background-color: white;
}
.h_item {
  margin: 18px 0;
  display: inline-block;
  width: 143.5px;
  text-align: center;
  font-size: 28px;
}

.h_img {
  margin: 64px 0;
}

.h_img image {
  width: 60px;
}

.h_item text {
  display: block;
}

.h_time {
  color: gray;
}

.h_wind_dir {
  margin-top: 32px;
}

.h_wind_sc {
  color: gray;
}

.h_tmp {
  color: #027aff;
}

.daily {
  width: 718px;
  white-space: nowrap;
  margin: 0 18px;
  background-color: white;
  border-radius: 18px;
  box-shadow: 0.1rem 0.1rem 0.5rem rgba(0, 0, 0, 0.15);
}

.d_item {
  margin: 18px 0;
  display: inline-block;
  width: 179.5px;
  text-align: center;
  font-size: 28px;
}

.d_item text {
  display: block;
}

.d_date {
  color: gray;
}

.d_wind_dir {
  margin-top: 32px;
}

.d_wind_sc {
  color: gray;
}

.footer {
  font-size: 28px;
  color: gray;
  text-align: center;
  margin-top: 50px;
  margin-bottom: 18px;
}
</style>
